﻿Imports System.Data
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Data.OleDb
Imports System
Imports System.Globalization

Partial Class Interfaz_frmBodegaSolicitar
    Inherits System.Web.UI.Page
    Dim CL As New ControladorLogica
    Dim dtDatos As New DataTable
    Dim dataset As New DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If CL.visibilidadesFormsInternos("Bodega", Session("user").ToString) = False Then
            Response.Redirect("Index.aspx")
        End If
        LabelUser.Text = Session("user").ToString
        Dim i As Integer
        Dim ArrayMaterialesBodega As New ArrayList
        Dim ArrayUsuarios As New ArrayList

        ArrayMaterialesBodega = CL.ObtenerMaterial()
        ArrayUsuarios = CL.ObtenerUsuario()

        If Not Page.IsPostBack Then
            For i = 0 To ArrayMaterialesBodega.Count - 1
                Me.DropMateriales.Items.Add(DirectCast(ArrayMaterialesBodega.Item(i), Material).Nombre)
            Next

            For i = 0 To ArrayUsuarios.Count - 1
                Me.DropUser.Items.Add(DirectCast(ArrayUsuarios.Item(i), Usuario).User)
            Next

        End If

        'se guardan datos en secion para el positback de la pagina respecto al los datos del datagridview
        If Session("SolicitudBodega") Is Nothing Then
            Session("SolicitudBodega") = dtDatos
        Else
            dtDatos = DirectCast(Session("SolicitudBodega"), DataTable)

        End If

        If Not Page.IsPostBack Then
            Try
                dtDatos.Columns.Add(New DataColumn("ITEM"))
                dtDatos.Columns.Add(New DataColumn("CANTIDAD"))

                Me.GridBodegaSolicitar.DataBind()
                Me.GridBodegaSolicitar.DataSource = Me.dtDatos

            Catch
            End Try
        End If
        ' Me.GridRequisicion.DataBind()
        Me.GridBodegaSolicitar.DataSource = Me.dtDatos
        FuncionAviso()
    End Sub

#Region "Eventos Grid"
    'Grid Material
    Protected Sub GridBodegaSolicitar_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        GridBodegaSolicitar.PageIndex = e.NewPageIndex
        'Bind data to the GridView control.
        GridBodegaSolicitar.DataSource = Session("SolicitudBodega")
        GridBodegaSolicitar.DataBind()
    End Sub

    Protected Sub GridBodegaSolicitar_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        'Set the edit index.
        GridBodegaSolicitar.EditIndex = e.NewEditIndex
        'Bind data to the GridView control.
        GridBodegaSolicitar.DataSource = Session("SolicitudBodega")
        GridBodegaSolicitar.DataBind()
    End Sub

    Protected Sub GridBodegaSolicitar_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
        'Reset the edit index.
        GridBodegaSolicitar.EditIndex = -1
        'Bind data to the GridView control.
        GridBodegaSolicitar.DataSource = Session("SolicitudBodega")
        GridBodegaSolicitar.DataBind()
    End Sub

    Protected Sub GridBodegaSolicitar_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        'Retrieve the table from the session object.
        Dim dt As DataTable = CType(Session("SolicitudBodega"), DataTable)

        'Update the values.
        Dim row As GridViewRow = GridBodegaSolicitar.Rows(e.RowIndex)
        'Dim actividad As String = (CType((row.Cells(2).Controls(0)), TextBox)).Text.ToString
        Dim CANTIDAD As String = (CType((row.Cells(2).Controls(0)), TextBox)).Text


        'dt.Rows(row.DataItemIndex)("ITEM") = actividad
        dt.Rows(row.DataItemIndex)("CANTIDAD") = CANTIDAD

        'Reset the edit index.
        GridBodegaSolicitar.EditIndex = -1

        'Bind data to the GridView control.
        GridBodegaSolicitar.DataSource = Session("SolicitudBodega")
        GridBodegaSolicitar.DataBind()
    End Sub

    Sub GridBodegaSolicitar_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        Dim dt As DataTable = CType(Session("SolicitudBodega"), DataTable)
        dt.Rows(e.RowIndex).Delete()
        GridBodegaSolicitar.DataSource = dt
        GridBodegaSolicitar.DataBind()
    End Sub
#End Region

#Region "Funcionalidades"
    Public Sub Mensaje(ByVal msg As String)
        Dim alertScript As String = "<script language=JavaScript runat=server>"
        alertScript &= "alert('" & msg & "');"
        alertScript &= "</script>"
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScrip", alertScript)
    End Sub

    Private Sub FuncionAviso()
        Dim query As String = "select count(*) cuenta from solicitudes where estado ='P'"
        Dim cantidadPendientes As Integer = CInt(CL.Ejecuta(query).Item("cuenta").ToString)

        If cantidadPendientes <> 0 Then
            Image2.Visible = True
            Image3.Visible = False
        Else
            Image2.Visible = False
            Image3.Visible = True
        End If
    End Sub

    Private Sub InsertarItem()
        Try
            Dim drDatos As DataRow = dtDatos.NewRow

            drDatos.Item("ITEM") = Me.DropMateriales.Text
            drDatos.Item("CANTIDAD") = Me.TxTCantidad.Text
            dtDatos.Rows.Add(drDatos)

            Me.GridBodegaSolicitar.DataSource = dtDatos
            Me.GridBodegaSolicitar.DataBind()

            Me.LimpiarItem()
            Me.GridBodegaSolicitar.ForeColor = Drawing.Color.Blue
        Catch ex As Exception
            Me.Mensaje(ex.ToString)
        End Try
    End Sub

    Private Sub LimpiarItem()
        Me.TxTCantidad.Text = ""
    End Sub

    Private Sub LimpiarTodo()
        Me.TxTCantidad.Text = ""
        Me.TxTDetalle.Text = ""
    End Sub

    Private Sub vaciaGrid()
        dtDatos = DirectCast(Session("SolicitudBodega"), DataTable)
        If Not dtDatos Is Nothing Then
            dtDatos.Clear()
            Session("SolicitudBodega") = dtDatos
        End If
        Me.GridBodegaSolicitar.DataSource = Nothing
        Me.GridBodegaSolicitar.DataBind()
    End Sub

    Private Sub SolicitarItemBodega()

        Dim filaGrid As GridViewRow
        Dim fechaSolicitud As String = basicFecha.Text
        Dim nombreSolicitante As String = DirectCast(CL.ObtenerUsuarioUserID(Me.DropUser.Text).Item(0), Usuario).ID
        Dim detalle As String = Me.TxTDetalle.Text
        Dim materialPedir As String = ""
        Dim cantidad As String = ""

        With Me.GridBodegaSolicitar
            For fila As Integer = 0 To .Rows.Count - 1
                filaGrid = .Rows(fila)

                materialPedir = DirectCast(CL.ObtenerMaterialNombre(filaGrid.Cells(1).Text).Item(0), Material).ID
                cantidad = filaGrid.Cells(2).Text

                Dim nuevaSolicitud As New Solicitud("", detalle, "Bodega", nombreSolicitante, fechaSolicitud, materialPedir, cantidad, "P")
                CL.insertarSolicitud(nuevaSolicitud)
            Next
        End With
    End Sub

    Private Function ValidaUsuario(ByVal userName As String, ByVal userPass As String) As Boolean
        Dim User As String = userName
        Dim Pass As String = userPass
        Dim Usuario As New Usuario(User, Pass)
        Dim Result As Boolean = CL.ValidarUsuario(Usuario)

        If Result = True Then
            TxTDetalle.ReadOnly = False
            TxTCantidad.ReadOnly = False
            BtnIngresar.Visible = True
            BtnSolicitar.Visible = True
            LblPass.Visible = False
            TextPass.Visible = False
            BtnAceptar.Visible = False
            LabelAviso.Visible = False
            Return True
        Else
            LabelAviso.Visible = True
            LabelAviso.Text = "Intentelo nuevamente"
            Return False
        End If

    End Function
#End Region

#Region "Botones"
    Protected Sub BtnLogout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnLogout.Click
        Session("user") = ""
        Response.Redirect("Principal.aspx")
    End Sub

    Protected Sub BtnIngresar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnIngresar.Click
        InsertarItem()
    End Sub

    Protected Sub BtnSolicitar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSolicitar.Click
        SolicitarItemBodega()
        LimpiarTodo()
        vaciaGrid()
        Me.Mensaje("Su solicitud a sido enviada")
    End Sub

    Protected Sub BtnHome_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnHome.Click
        Response.Redirect("index.aspx")
    End Sub

    Protected Sub BtnValidar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnValidar.Click
        Me.LblPass.Visible = True
        Me.TextPass.Visible = True
        Me.BtnAceptar.Visible = True
    End Sub

    Protected Sub BtnAceptar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnAceptar.Click
        ValidaUsuario(Me.DropUser.Text, TextPass.Text)
    End Sub
#End Region

  
End Class
